package oop;

/**
 * @author: knowno
 * @create: 2025-09-11 09:33
 * @Version 1.0
 **/
public class TestExtends {

    public static void main(String[] args) {
        //Student类中没有任何代码
        //子类构造会调用父类的构造方法
        Student s1 = new Student("小白",18);
        //setXX的方法是从父类中继承过来的
//        s1.setName("张三");
//        s1.setAge(18);
        System.out.println(s1);
        //从Object继承的方法
        //多层继承关系: Student->Person->Object
        System.out.println(s1.hashCode());

        //'study()' has private access in 'oop.Person'
        //s1.study();
        //子类不能直接访问父类的私有属性
        //System.out.println(s1.name);
        //System.out.println(s1.age);


    }

}

class Student extends Person{
    //子类的无参构造会默认调用父类的无参构造
    public Student(String name,int age) {
        super(name,age);
        System.out.println("子类的无参构造");
    }



}

class Teacher extends Person{
    public Teacher() {
        //显式调用父类的带参数构造
        super("teacher",18);
        System.out.println("子类的无参构造");
    }

}

class worker extends Person{
    public worker() {
        //显式调用父类的带参数构造
        super("worker",18);
        System.out.println("子类的无参构造");

    }

}

/**
 * 父类
 */
class  Person{
    private String name;
    private int age;

//    public Person() {
//        System.out.println("父类的无参构造");
//    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println("父类的有参构造");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    private void study(){
        System.out.println("学习");
    }
}
